import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import useStore from "@/store/store";
import {ElNotification} from "element-plus";
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    // history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'Login',
            component: () => import('@/views/Login.vue'),
            meta: {
                title: '登录'
            }
        },
        {
            path: '/user_info',
            name: 'Home',
            component: () => import('@/views/Home.vue'),
            children: [
                {
                    path: '',
                    component: () => import('@/components/Home/user_info.vue'),
                    meta: {
                        title: '教师个人基本信息'
                    }
                },
                {
                    path: '/basic_teaching_work',
                    component: () => import('@/components/Home/basic_teaching_work.vue'),
                    meta: {
                        title: '基础教学工作'
                    }
                },
                {
                    path: '/Professional_and_course_building_outcomes',
                    component: () => import('@/components/Home/Professional_and_course_building_outcomes.vue'),
                    meta: {
                        title: '专业和课程建设成果'
                    }
                },
                {
                    path: '/Scientific_Research_Contest_and_Integrated_Curriculum_Reform',
                    component: () => import('@/components/Home/Scientific_Research_Contest_and_Integrated_Curriculum_Reform.vue'),
                    meta: {
                        title: '科研大赛与一体化课改'
                    }
                },
                {
                    path: '/Enterprise_practice_exercise',
                    component: () => import('@/components/Home/Enterprise_practice_exercise.vue'),
                    meta: {
                        title: '企业实践锻炼'
                    }
                },
                {
                    path: '/export',
                    component: () => import('@/components/Home/export.vue'),
                    meta: {
                        title: '导出'
                    }
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const store = useStore()
    if (to.name === 'Login') {
        if (store.user_id != null && store.user_id !== '') {
            next('/user_info')
            ElNotification({
                type: "success",
                title: `欢迎回来，${store.user_name}`,
                message: '检测到您之前登陆过，系统已自动登录'
            })
        }
        next()
    } else {
        if (store.user_id != null && store.user_id !== ''){
            next()
        } else {
            next('/')
        }
    }
    document.title = `${to.meta.title} - 教师档案管理系统` || '教师档案管理系统'
})

export default router
